package com.kevin.Code.Array;

/**
 * @author Vinlee Xiao
 * @Classname ProductOfArrayExceptSelf
 * @Description 238. 除自身以外数组的乘积 中等难度  完全没啥思路 hot100
 * @Date 2021/11/19 16:02
 * @Version 1.0
 */
public class ProductOfArrayExceptSelf {

    /**
     * @param nums
     * @return
     */
    public int[] productExceptSelf1(int[] nums) {
        int len = nums.length;

        //新建一个left数组用于记录当前元素坐标的成绩之和
        int[] left = new int[len];
        int[] right = new int[len];
        int[] result = new int[len];

        left[0] = 1;
        for (int i = 1; i < len; i++) {
            left[i] = left[i - 1] * nums[i - 1];
        }

        right[len - 1] = 1;
        for (int i = len - 2; i >= 0; i--) {
            right[i] = right[i + 1] * nums[i + 1];
        }

        for (int i = 0; i < len; i++) {
            result[i] = left[i] * right[i];

        }
        return result;
    }

    public int[] productExceptSelf2(int[] nums) {
        int len = nums.length;
        //新建一个left数组用于记录当前元素坐标的成绩之和
        int[] left = new int[len];

        left[0] = 1;
        for (int i = 1; i < len; i++) {
            left[i] = left[i - 1] * nums[i - 1];
        }
        int right = 1;
        for (int i = len - 1; i >= 0; i--) {
            left[i] = right * left[i];
            right = right * nums[i];
        }

        return left;
    }


    public int[] productExceptSelf3(int[] nums) {
        int len = nums.length;
        //新建一个left数组用于记录当前元素坐标的成绩之和
        int[] right = new int[len];

        right[len - 1] = 1;
        for (int i = len - 2; i >= 0; i--) {
            right[i] = right[i + 1] * nums[i + 1];
        }
        int left = 1;
        for (int i = 0; i < len; i++) {
            right[i] = left * right[i];
            left = left * nums[i];
        }

        return right;
    }
}
